class GenerateJsonWebpackPlugin {
  constructor(options) {
    this.filename = options.filename;
    this.fn = options.fn;
  }

  apply(compiler) {
    compiler.hooks.emit.tapAsync(
      "GenerateJsonWebpackPlugin",
      (compilation, callback) => {
        this.fn(compilation, (err, body) => {
          if (err) {
            return callback(err);
          }

          compilation.assets[this.filename] = {
            source: () => body,
            size: () => body.length
          };

          callback();
        });
      }
    );
  }
}

module.exports = GenerateJsonWebpackPlugin;
